package com.zwk.gulimall.coupon.service.impl;

import com.zwk.common.to.MemberPrice;
import com.zwk.common.to.SkuReductionTo;
import com.zwk.gulimall.coupon.entity.MemberPriceEntity;
import com.zwk.gulimall.coupon.entity.SkuLadderEntity;
import com.zwk.gulimall.coupon.service.MemberPriceService;
import com.zwk.gulimall.coupon.service.SkuLadderService;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwk.common.utils.PageUtils;
import com.zwk.common.utils.Query;

import com.zwk.gulimall.coupon.dao.SkuFullReductionDao;
import com.zwk.gulimall.coupon.entity.SkuFullReductionEntity;
import com.zwk.gulimall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
    @Autowired
    private MemberPriceService memberService;
    @Autowired
    private SkuLadderService ladderService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {
        SkuLadderEntity ladderEntity = new SkuLadderEntity();
        BeanUtils.copyProperties(skuReductionTo,ladderEntity);
        ladderEntity.setAddOther(skuReductionTo.getCountStatus());
        ladderEntity.setFullCount(skuReductionTo.getFullCount());
        ladderEntity.setDiscount(skuReductionTo.getDiscount());
        ladderEntity.setSkuId(skuReductionTo.getSkuId());
        ladderService.save(ladderEntity);

        SkuFullReductionEntity reductionEntity = new SkuFullReductionEntity();
        reductionEntity.setAddOther(skuReductionTo.getPriceStatus());
        reductionEntity.setFullPrice(skuReductionTo.getFullPrice());
        reductionEntity.setReducePrice(skuReductionTo.getReducePrice());
        reductionEntity.setSkuId(skuReductionTo.getSkuId());
        this.save(reductionEntity);

        List<MemberPriceEntity> memberPrices = skuReductionTo.getMemberPrice().stream().map(item -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
            memberPriceEntity.setMemberLevelId(item.getId());
            memberPriceEntity.setMemberLevelName(item.getName());
            memberPriceEntity.setMemberPrice(item.getPrice());
            memberPriceEntity.setAddOther(1);   //默认所有会员可叠加
            return memberPriceEntity;
        }).collect(Collectors.toList());
        memberService.saveBatch(memberPrices);
    }

}